package link.luyu.demo.port;

import java.util.List;
import link.luyu.demo.port.db.LuyuHashLockTableBean;
import link.luyu.demo.port.db.LuyuHashLockTableJPA;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HashLockManager {
    private static Logger logger = LoggerFactory.getLogger(HashLockManager.class);

    private LuyuHashLockTableJPA jpa;

    public HashLockManager(LuyuHashLockTableJPA jpa) {
        logger.info("Constructing HashLockManager ...");
        this.jpa = jpa;
    }

    public LuyuHashLockTableBean getBeanByCargoId(String cid) {
        synchronized (this) {
            List<LuyuHashLockTableBean> rows = jpa.findByCargoId(cid);
            if (rows != null && rows.size() > 0) {
                return rows.get(0);
            }
            return null;
        }
    }

    public List<LuyuHashLockTableBean> getMyExportCargo(String from, String status) {
        synchronized (this) {
            return jpa.findByFromChainPathAndStatus(from, status);
        }
    }

    public List<LuyuHashLockTableBean> getMyImportCargo(String to, String status) {
        synchronized (this) {
            return jpa.findByToChainPathAndStatus(to, status);
        }
    }

    public void saveBean(LuyuHashLockTableBean bean) {
        synchronized (this) {
            jpa.save(bean);
        }
    }
}
